package cn.btks.commonserver.util;

import cn.btks.commonserver.result.Constants;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cheng-qiang
 * @date 2020年08月21日 13:04
 * @参考资料：
 * @Description:
 */
public class DateUtils {

    /**
     * 日期之差得天数
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getDaySub(Date beginDate, Date endDate){
        if (null == beginDate || null == endDate){return Constants.GENERAL_STATUS_CODE_ZERO;}
        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.YEAR_MONTH_DAY);
        String beginDateStr = dateFormat.format(beginDate);
        String endDateStr = dateFormat.format(endDate);
        long day = 0;
        try {
            Date begin = dateFormat.parse(beginDateStr);
            Date end = dateFormat.parse(endDateStr);
            day =  (end.getTime()-begin.getTime())/(24*60*60*1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day+Constants.GENERAL_STATUS_CODE_ONE;
    }

    /**
     * 判断日期是否大于指定日期
     * @param productEndDate
     * @param planEndDate
     * @return
     */
    public static boolean getDateSize(Date planEndDate,Date productEndDate){
        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.YEAR_MONTH_DAY);
        String formatProductEndDate = dateFormat.format(productEndDate);
        String formatPlanEndDate = dateFormat.format(planEndDate);
        boolean b = false;
        try {
            Date parseProductEndDate = dateFormat.parse(formatProductEndDate);
            Date parsePlanEndDate = dateFormat.parse(formatPlanEndDate);
            int i = parsePlanEndDate.compareTo(parseProductEndDate);
            if (i == Constants.GENERAL_STATUS_CODE_ONE){
                b = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 判断日期是否等于指定日期
     * @param productEndDate
     * @param planEndDate
     * @return
     */
    public static boolean getDateEquals(Date planEndDate,Date productEndDate){
        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.YEAR_MONTH_DAY);
        String formatProductEndDate = dateFormat.format(productEndDate);
        String formatPlanEndDate = dateFormat.format(planEndDate);
        boolean b = false;
        try {
            Date parseProductEndDate = dateFormat.parse(formatProductEndDate);
            Date parsePlanEndDate = dateFormat.parse(formatPlanEndDate);
            int i = parsePlanEndDate.compareTo(parseProductEndDate);
            if (i == Constants.GENERAL_STATUS_CODE_ZERO){
                b = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 获取日期的年月日
     * @param date
     * @return
     */
    public static Map<String,String> getDateString(Date date){
        Map<String,String> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH);//获取日
        int day = calendar.get(Calendar.DATE);//获取日
        map.put("year",String.valueOf(year));
        map.put("month",String.valueOf(month));
        map.put("day",String.valueOf(day));
        return map;
    }

    /**
     * 获取日期的时分秒
     * @param time
     * @return
     */
    public static Map<String,String> getTimeString(Time time){
        Map<String,String> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//时（24小时制）
        int minute = calendar.get(Calendar.MINUTE);//分
        int second = calendar.get(Calendar.SECOND);//秒
        map.put("hour",String.valueOf(hour));
        map.put("minute",String.valueOf(minute));
        map.put("second",String.valueOf(second));
        return map;
    }


//    /**
//     * 测试~
//     * @param args
//     * @throws ParseException
//     */
//    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.YEAR_MONTH_DAY_TIME);
//        String startDateStr = "2020-09-07 12:00:00";
//        Date parse = dateFormat.parse(startDateStr);
//        long daySub = getDaySub(null, null);
//        System.out.println(daySub);
//    }
}
